package b2.widget.warehouse
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.player.dataModel.Mail;
	import b2.player.dataModel.ObjWithNum;
	import b2.player.dataModel.Tank;
	import b2.scene.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.sendMail.SendMailTab;
	import b2.widget.tab.*;
	import b2.widget.tank.*;
	import b2.widget.tech.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.Point;
	import flash.text.*;
	import flash.ui.Mouse;
	
	public class TankItem extends Sprite
	{
		//坦克唯一号
		private var tankId:int;
		
		//0, 表示仓库; 1, 表示科技塔 
		public var location:int;
		//拖拽前的父容器
		private var oldParent:DisplayObjectContainer;
		//拖拽前的位置x
		private var oldPosX:Number;
		//拖拽前的位置y
		private var oldPosY:Number;
		
		public function TankItem(tankId:int, location:int = 0)
		{
			super();
			this.tankId = tankId;
			this.location = location;

			var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankId];
			var tankConfig:TankConfig = Config.objectConfigs[tankInfo.index];
			//背景框
			var ObjectItemBgr:Class = ResourceHelper.getInstance().getClassByName("ObjectItemBgr" + tankInfo.basicLevel);
			var objectItemBgr:Sprite = new ObjectItemBgr();
			objectItemBgr.width = 100;
			objectItemBgr.height = 100;
			addChild(objectItemBgr);
			
//			//环绕发光
//			var CircleLight:Class = ResourceHelper.getInstance().getClassByName("CircleLight");
//			var circleLight:Sprite = new CircleLight();
//			circleLight.width = 72;
//			circleLight.height = 106;
//			addChild(circleLight);
			//坦克图片
			var TankImage:Class = ResourceHelper.getInstance().getClassByName(tankConfig.iconImage);
			var tankSprite:Sprite = new TankImage();
			//齐天大圣
			if (tankInfo.index == 10015)
			{
				tankSprite.width = 110;
				tankSprite.height = 123;
			}
			//死亡骑士
			else if (tankInfo.index == 10020)
			{
				tankSprite.width = 96;
				tankSprite.height = 96;
			}
			else
			{
				tankSprite.width = 80;
				tankSprite.height = 80;
			}
			tankSprite.x = 10;
			tankSprite.y = 10;
			addChild(tankSprite);
			//显示星级
			var levelText:TextField = new TextField();
			levelText.selectable = false;
        	levelText.autoSize = TextFieldAutoSize.LEFT;
			levelText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0xFFFFFF);
			levelText.text = Config.basicConfig.tankStrengthenLevel[tankInfo.strengthenLevel];
			levelText.x = (100 - levelText.width) - 2;
			levelText.y = 0;
			addChild(levelText);
			//出征文字
			var me:PlayerInfo = PlayerInfoManager.getInstance().players[PlayerInfoManager.getInstance().myUserId];
			if (me.campTankId == tankId)
			{
				var campText:TextField = new TextField();
				campText.selectable = false;
	        	campText.autoSize = TextFieldAutoSize.CENTER;
				campText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0xFFFF00, true);
				campText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
				campText.text = "出战中";
				campText.x = 30;
				campText.y = 75;
				addChild(campText);
			}
			this.mouseChildren = false;
			this.doubleClickEnabled = true;
			addEventListener(MouseEvent.DOUBLE_CLICK, onMouseDoubleClick);
			addEventListener(MouseEvent.CLICK, onMouseClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		private function onMouseClick(event:MouseEvent):void
		{
			if (TankDialog.getInstance().parent != null)
			{
				TankDialog.getInstance().fresh(this.tankId);
			}
		}
		
		private function onMouseDoubleClick(event:MouseEvent):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			var tankInfo:TankInfo = PlayerInfoManager.getInstance().tanks[tankId];
			var tankConfig:TankConfig = Config.objectConfigs[tankInfo.index];
			if (TechTower.getInstance().parent != null)
			{
				var techTab:TechTab = TechTower.getInstance().tabs[TechTower.getInstance().currentTabIndex] as TechTab;
				if (this.location == 0)
				{
					techTab.loadObject(tankInfo.index,tankInfo.basicLevel,tankInfo.strengthenLevel,tankId);
				}
				else if (this.location == 1)
				{
					techTab.removeAll();
				}
				Warehouse.getInstance().refresh();
			}
			else if (SendMailTab.getInstance().parent != null)
			{
				var sendMailTab:SendMailTab = SendMailTab.getInstance();
				var mail:Mail = sendMailTab.mail;
				if (this.location == 0)
				{
					if (me.campTankId == tankId)
					{
						var popMessage:PopMessage = new PopMessage("出战坦克不能赠送", 0xff0000);
						WidgetManager.getInstance().popOut([popMessage], false, true);
					}
					else if (me.isAdmin || tankConfig.priceGas == 0)
					{
						var gift:ObjWithNum = new ObjWithNum(new Tank(tankInfo.index,tankInfo.basicLevel,tankInfo.strengthenLevel,tankId),1);
						mail.addGift(gift);
						Warehouse.getInstance().refresh();
					}
					else
					{
						popMessage = new PopMessage("该坦克不能互相赠送！", 0xff0000);
						WidgetManager.getInstance().popOut([popMessage], false, true);
					}
				}
			}
		}
		
		private function onChangeEquipComplete(event:ServerEvent):void
		{
			if (TankDialog.getInstance().parent != null)
			{
				var targetTankId:int = TankDialog.getInstance().tankSlot.tankId;
				TankDialog.getInstance().fresh(targetTankId);
				Warehouse.getInstance().refresh();
			}
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(BFont.brightFilter)];
			//没有拖拽
			if (this.parent == SceneManager.getInstance().currentScene)
				return;
			var tankMes:TankMessage = new TankMessage(this.tankId);
			var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
			tankMes.x = globalPoint.x + this.width;
			tankMes.y = globalPoint.y;
			if (tankMes.x + tankMes.width > BFont.stageWidth)
				tankMes.x = globalPoint.x - tankMes.width;
			if (tankMes.y + tankMes.height > BFont.stageHeight)
				tankMes.y = BFont.stageHeight - tankMes.height;
			WidgetManager.getInstance().popOutMouseOverMessage(tankMes);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
			var mouseOverMessage:Sprite = WidgetManager.getInstance().mouseOverMessage;
			if (mouseOverMessage != null)
			{
				if (event.stageX < mouseOverMessage.x || event.stageX > mouseOverMessage.x + mouseOverMessage.width || 
					event.stageY < mouseOverMessage.y || event.stageY > mouseOverMessage.y + mouseOverMessage.height)
				{
					WidgetManager.getInstance().closeMouseOverMessage(); 
				}
			}
		}
		
		//开始拖拽
		private function onMouseDown(event:MouseEvent):void
		{
			if (this.parent != SceneManager.getInstance().currentScene)
			{ 
				var globalPoint:Point = this.parent.localToGlobal(new Point(this.x, this.y));
				this.oldPosX = this.x;
				this.oldPosY = this.y;
				this.oldParent = this.parent;
				this.x = globalPoint.x;
				this.y = globalPoint.y;
				SceneManager.getInstance().currentScene.addChild(this);
				WidgetManager.getInstance().closeMouseOverMessage();
				this.startDrag();
			}
		}
		
		//拖拽结束
		private function onMouseUp(event:MouseEvent):void
		{
			this.stopDrag();
			if (this.parent != null)
				this.parent.removeChild(this);
			var parentWidget:Sprite = Warehouse.getInstance();
			//如果拖拽没有离开原来的父容器
			if (this.x >= parentWidget.x && this.x < parentWidget.x + parentWidget.width &&
				this.y >= parentWidget.y && this.y < parentWidget.y + parentWidget.height)
			{
				//还原原来的位置
				this.x = this.oldPosX;
				this.y = this.oldPosY;
				if (this.oldParent != null)
				{
					this.oldParent.addChild(this);
					this.oldParent = null;
				}
			}
			else
			{
				if (TankDialog.getInstance().parent != null)
				{
					onMouseClick(event);
				}
				else if (TechTower.getInstance().parent != null)
				{
					onMouseDoubleClick(event);
				}
				Warehouse.getInstance().refresh();
			}
		}
	}
}